//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package project_review_dao

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/project_review_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type ReviewLinkItemDbItem struct {
	LinkId       string `bson:"_id"`
	ProjectId    string `bson:"projectId"`
	ReviewId     string `bson:"reviewId"`
	LinkType     uint32 `bson:"linkType"`
	LinkTargetId string `bson:"linkTargetId"`
	TimeStamp    int64  `bson:"timeStamp"`
}

func (item *ReviewLinkItemDbItem) ToReviewLinkItem() *project_review_api.ReviewLinkItem {
	return &project_review_api.ReviewLinkItem{
		LinkId:       item.LinkId,
		LinkType:     project_review_api.LINK_TYPE(item.LinkType),
		LinkTargetId: item.LinkTargetId,
		TimeStamp:    item.TimeStamp,
	}
}

type _LinkDao struct {
	mc *mongo.Collection
}

func newLinkDao(db *mongo.Database) (*_LinkDao, error) {
	mc := db.Collection(db_ns.DB_NS_PROJECT_REVIEW + "link")
	_, err := mc.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
		{
			Keys: bson.D{{"projectId", 1}, {"reviewId", 1}},
		},
		{
			Keys: bson.D{{"projectId", 1}, {"linkTargetId", 1}},
		},
	})
	if err != nil {
		return nil, err
	}
	return &_LinkDao{mc: mc}, nil
}

func (db *_LinkDao) Insert(ctx context.Context, dbItem *ReviewLinkItemDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_LinkDao) Exist(ctx context.Context, projectId, reviewId, linkId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": linkId, "projectId": projectId, "reviewId": reviewId})
	return count > 0, err
}

func (db *_LinkDao) Get(ctx context.Context, projectId, reviewId, linkId string) (*ReviewLinkItemDbItem, error) {
	retItem := &ReviewLinkItemDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": linkId, "projectId": projectId, "reviewId": reviewId}).Decode(retItem)
	return retItem, err
}

func (db *_LinkDao) Remove(ctx context.Context, projectId, reviewId, linkId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": linkId, "projectId": projectId, "reviewId": reviewId})
	return err
}

func (db *_LinkDao) RemoveByReview(ctx context.Context, projectId, reviewId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"projectId": projectId, "reviewId": reviewId})
	return err
}

func (db *_LinkDao) RemoveByTarget(ctx context.Context, projectId, linkTargetId string, linkType project_review_api.LINK_TYPE) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"projectId": projectId, "linkType": uint32(linkType), "linkTargetId": linkTargetId})
	return err
}

func (db *_LinkDao) List(ctx context.Context, projectId string, reviewIdList []string) ([]*ReviewLinkItemDbItem, error) {
	if len(reviewIdList) == 0 {
		return []*ReviewLinkItemDbItem{}, nil
	}
	cursor, err := db.mc.Find(ctx, bson.M{"projectId": projectId, "reviewId": bson.M{"$in": reviewIdList}}, options.Find().SetSort(bson.M{"timeStamp": 1}))
	if err != nil {
		return nil, err
	}
	retList := []*ReviewLinkItemDbItem{}

	err = cursor.All(ctx, &retList)
	return retList, err
}
